package year2022.month456789;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;

public class DateTimeTest {

    public static void main(String[] args){
        LocalDateTime day = LocalDateTime.of(2022, 6, 1, 0, 0);
        System.out.println(day);
        System.out.println("========================");

        String yearMonth = "2022-07";
        if (StrUtil.isNotBlank(yearMonth)) {
            String[] arr = yearMonth.split("-");
            LocalDateTime dateTime = LocalDateTime.of(Integer.parseInt(arr[0]), Integer.parseInt(arr[1]), 1, 0, 0);

            LocalDateTime yearMonthFirstDay = dateTime.with(TemporalAdjusters.firstDayOfMonth()).withHour(0).withMinute(0).withSecond(0);
            LocalDateTime yearMonthLastDay = dateTime.with(TemporalAdjusters.lastDayOfMonth()).withHour(23).withMinute(59).withSecond(59);
            System.out.println(yearMonthFirstDay);
            System.out.println(yearMonthLastDay);
            System.out.println("========================");

            yearMonthFirstDay = LocalDateTimeUtil.offset(dateTime.with(TemporalAdjusters.lastDayOfMonth()).withHour(23).withMinute(59).withSecond(59), -1, ChronoUnit.MONTHS);
            yearMonthLastDay = LocalDateTimeUtil.offset(dateTime.with(TemporalAdjusters.firstDayOfMonth()).withHour(0).withMinute(0).withSecond(0), 1, ChronoUnit.MONTHS);
            System.out.println(yearMonthFirstDay);
            System.out.println(yearMonthLastDay);
            System.out.println("========================");

        }

        int year = 2022;
        int month = 9;
        // 该月第一天
        LocalDateTime firstDay = null;
        // 该月最后一天
        LocalDateTime lastDay = null;
        LocalDateTime yearMonth0 = LocalDateTime.of(year, month, 1, 0, 0);
        firstDay = LocalDateTimeUtil.offset(yearMonth0.with(TemporalAdjusters.lastDayOfMonth()).withHour(23).withMinute(59).withSecond(59), -1, ChronoUnit.MONTHS);
        lastDay = LocalDateTimeUtil.offset(yearMonth0.with(TemporalAdjusters.firstDayOfMonth()).withHour(0).withMinute(0).withSecond(0), 1, ChronoUnit.MONTHS);
        System.out.println(firstDay);
        System.out.println(lastDay);

    }

}
